package com.ww.java8.fp.stream.inaction.ch05;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ww.java8.fp.stream.inaction.ch04.Dish;

/**
 * map、flatMap
 */
public class MapAndFlatMapDemo {

    public static void main(String[] args) {
        List<String> dishNames = Dish.menu.stream()
        	.map(Dish::getName)
        	.collect(Collectors.toList());
        System.out.println(dishNames);
        System.out.println("------------");
        
        List<String> words = Arrays.asList("Hello", "World");
        List<Integer> wordLens = words.stream()
        	.map(String::length)
        	.collect(Collectors.toList());
        System.out.println(wordLens);
        System.out.println("------------");
        
        /**
         * flatMap
         */
        words.stream()
        	.flatMap((String line) -> Arrays.stream(line.split("")))
        	.distinct()
        	.forEach(System.out::print);
        System.out.println("------------");
        
        List<Integer> numbers1 = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> numbers2 = Arrays.asList(6, 7, 8, 11);
        List<int[]> pairs = numbers1.stream()
        	.flatMap((Integer i) -> numbers2.stream().map((Integer j) -> new int[] { i, j }))
        	.filter(pair -> 0 == (pair[0] + pair[1]) % 3)
        	.collect(Collectors.toList());
       pairs.forEach(pair -> System.out.println("(" + pair[0] + ", " + pair[1] + ")"));
    }
}
